
/*##############################################################################
 Thomas M. Flaherty
 Email: t1flaher@ucsd.edu
 Date Created: 09/29/2019
##############################################################################*/


clear    
clear matrix
set matsize 10000
set more off
capture log close 
set scrollbufsize 300000 // (set scrollbufsize will take effect the next time you launch Stata)

*###############################################################################
* change directory 
*###############################################################################

cd "~/Dropbox/replication_files/

*###############################################################################
* import and merge raw inequality data from WID (downloaded June 13th, 2020)
*###############################################################################

* top 1
import excel using "data/input/world_inequality_database/WID_aptinc_p90p100_13062020-155452.xlsx"
drop B C 
rename A country
rename D year
rename E avepretaxincp90p100
save "data/input/world_inequality_database/WID_aptinc_p90p100_13062020-155452.dta", replace

* top 10-40
clear
import excel using "data/input/world_inequality_database/WID_aptinc_p50p90_13062020-155305.xlsx"
drop B C 
rename A country
rename D year
rename E avepretaxincp50p90
save "data/input/world_inequality_database/WID_aptinc_p50p90_13062020-155305.dta", replace

* top 10
clear
import excel using "data/input/world_inequality_database/WID_aptinc_p99p100_13062020-142745.xlsx"
drop B C 
rename A country
rename D year
rename E avepretaxincp99p100
save "data/input/world_inequality_database/WID_aptinc_p99p100_13062020-142745.dta", replace

* top 90-99
clear
import excel using "data/input/world_inequality_database/WID_afiinc_p90p99_13062020-144327.xlsx"
drop B C 
rename A country
rename D year
rename E avepretaxincp90p99
save "data/input/world_inequality_database/WID_afiinc_p90p99_13062020-144327.dta", replace
clear
import excel using "data/input/world_inequality_database/WID_aptinc_p90p99_13062020-144246.xlsx"
drop B C 
rename A country
rename D year
rename E avepretaxincp90p99
drop if country=="USA"
append using "data/input/world_inequality_database/WID_afiinc_p90p99_13062020-144327.dta"
save "data/input/world_inequality_database/WID_aptinc_p90p99_13062020-144246.dta", replace

use "data/input/world_inequality_database/WID_aptinc_p90p100_13062020-155452.dta", clear
merge 1:1 country year using "data/input/world_inequality_database/WID_aptinc_p50p90_13062020-155305.dta"
drop _merge
merge 1:1 country year using "data/input/world_inequality_database/WID_aptinc_p99p100_13062020-142745.dta"
drop _merge
merge 1:1 country year using "data/input/world_inequality_database/WID_aptinc_p90p99_13062020-144246.dta"
drop _merge

* create complete panel
gen missing = 1 if avepretaxincp90p100==. | avepretaxincp50p90==.
drop if missing==1
drop missing
drop if year<=1990
drop if year>2014
drop if country=="Sweden"

* log
gen l_avepretaxincp90p100 = ln(avepretaxincp90p100)
gen l_avepretaxincp50p90 = ln(avepretaxincp50p90)
gen l_avepretaxincp99p100 = ln(avepretaxincp99p100) 
gen l_avepretaxincp90p99 = ln(avepretaxincp90p99) 

* create a var that identifies the index year observation
save temp.dta, replace
gen l_p90p100baseyear = l_avepretaxincp90p100 if year==1991
gen l_p50p90baseyear = l_avepretaxincp50p90 if year==1991
gen l_p99p100baseyear = l_avepretaxincp99p100 if year==1991
gen l_p90p99baseyear = l_avepretaxincp90p99 if year==1991
keep country l_p90p100baseyear l_p50p90baseyear l_p99p100baseyear l_p90p99baseyear
drop if l_p90p100baseyear==.
save temp2.dta, replace
use temp.dta, clear
merge m:1 country using temp2.dta
erase temp.dta
erase temp2.dta
drop _merge

egen country_id = group(country)
tsset country_id year
gen Dl_avepretaxincp90p100  = 100*(l_avepretaxincp90p100 - l_p90p100baseyear)
gen Dl_avepretaxincp50p90  = 100*(l_avepretaxincp50p90 - l_p50p90baseyear)
gen Dl_avepretaxincp99p100  = 100*(l_avepretaxincp99p100 - l_p99p100baseyear)
gen Dl_avepretaxincp90p99  = 100*(l_avepretaxincp90p99 - l_p90p99baseyear)

tssmooth ma MAl_avepretaxincp90p100 = Dl_avepretaxincp90p100, window(3,1,0)
tssmooth ma MAl_avepretaxincp50p90 = Dl_avepretaxincp50p90, window(3,1,0)
tssmooth ma MAl_avepretaxincp99p100 = Dl_avepretaxincp99p100, window(3,1,0)
tssmooth ma MAl_avepretaxincp90p99 = Dl_avepretaxincp90p99, window(3,1,0)


*###############################################################################
* create combined graph
*###############################################################################   
	   
twoway (line MAl_avepretaxincp99p100 year if country=="France") ///
	   (line MAl_avepretaxincp99p100 year if country=="Germany") /// 
	   (line MAl_avepretaxincp99p100 year if country=="Italy") /// 
	   (line MAl_avepretaxincp99p100 year if country=="USA") /// 
	   (line MAl_avepretaxincp99p100 year if country=="United Kingdom") ///
	   , legend(size(small) cols(1) label(1 "France") label(2 "Germany") label(3 "Italy") label(4 "USA") label(5 "United Kingdom"))  ///
       title("Top 1%") ///
	   xtitle("") ///
	   ytitle("") ///
	   ylabel(-10(20)90, angle(0) nogrid) ///
	   name(top1, replace) ///
	   graphregion(color(white)) bgcolor(white) ///
	   scheme(s2mono)
	   
twoway (line MAl_avepretaxincp90p99 year if country=="France") ///
	   (line MAl_avepretaxincp90p99 year if country=="Germany") /// 
	   (line MAl_avepretaxincp90p99 year if country=="Italy") /// 
	   (line MAl_avepretaxincp90p99 year if country=="USA") /// 
	   (line MAl_avepretaxincp90p99 year if country=="United Kingdom") ///
	   , legend(label(1 "France") label(2 "Germany") label(3 "Italy")  label(4 "USA") label(5 "United Kingdom"))  ///
       title("Next 9%") /// 
	   xtitle("") ///
	   ytitle("") ///
	   ylabel(-10(20)90, angle(0) nogrid) ///
	   name(top9099, replace) ///
	   graphregion(color(white)) bgcolor(white) ///
	   scheme(s2mono)
	   
twoway (line MAl_avepretaxincp50p90 year if country=="France") ///
	   (line MAl_avepretaxincp50p90 year if country=="Germany") /// 
	   (line MAl_avepretaxincp50p90 year if country=="Italy") /// 
	   (line MAl_avepretaxincp50p90 year if country=="USA") /// 
	   (line MAl_avepretaxincp50p90 year if country=="United Kingdom") ///
	   , legend(label(1 "France") label(2 "Germany") label(3 "Italy")  label(4 "USA") label(5 "United Kingdom"))  ///
       title("Middle 40%") ///
	   xtitle("") ///
	   ytitle("")  ///
	   ylabel(-10(20)90, angle(0) nogrid) ///
	   name(top5090, replace) ///	   
	   graphregion(color(white)) bgcolor(white) ///
	   scheme(s2mono)

grc1leg top1 top9099 top5090,  legendfrom(top1) position(4) ring(0)
graph export "figures/Changes in Real Income by percentile_bw.png", replace


*###############################################################################
* END
*###############################################################################   


